package com.lzc.tlmail.tlmailproduct.app;

import java.util.List;
import java.util.Map;


import com.lzc.tlmail.tlmailproduct.entity.AttrEntity;
import com.lzc.tlmail.tlmailproduct.service.AttrAttrgroupRelationService;
import com.lzc.tlmail.tlmailproduct.service.AttrService;
import com.lzc.tlmail.tlmailproduct.service.CategoryService;
import com.lzc.tlmail.tlmailproduct.vo.AttrattrgrouprelationVo;
import com.lzc.tlmail.tlmailproduct.vo.AttrgroupwithattrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lzc.tlmail.tlmailproduct.entity.AttrGroupEntity;
import com.lzc.tlmail.tlmailproduct.service.AttrGroupService;
import com.lzc.common.utils.PageUtils;
import com.lzc.common.utils.R;



/**
 * 属性分组
 *
 * @author lzc
 * @email 1029669636@gmail.com
 * @date 2020-07-25 21:57:44
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;

    /**
     * /product/attrgroup/{attrgroupId}/attr/relation
     * 获取属性分组关联的属性
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R getAttrRelation(@PathVariable("attrgroupId") Long attrgroupId){
         List<AttrEntity> attrEntities=attrService.attrServicegetAttrById(attrgroupId);
         return R.ok().put("data",attrEntities);
    }

    /**
     * 获取属性分组没有关联的属性
     * /product/attrgroup/{attrgroupId}/noattr/relation
     */

    @GetMapping("{attrgroupId}/noattr/relation")
    public R getNoAttrRelation(@PathVariable Long attrgroupId,
                               @RequestParam Map<String, Object> params){
       PageUtils pageUtils=attrService.getNoAttrRelation(params,attrgroupId);
       return R.ok().put("page",pageUtils);
    }

    /**
     * 添加属性分组 和关联关系
     * /product/attrgroup/attr/relation
     */
    @PostMapping("/attr/relation")
    public R saveAttrRelation(@RequestBody List<AttrattrgrouprelationVo> attrattrgrouprelationVo){
        attrAttrgroupRelationService.saveBatch(attrattrgrouprelationVo);
        return R.ok();
    }
    /**
     * /product/attrgroup/{catelogId}/withattr 查询分类下的所有分组和分组的属性
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrGroupAndAttrByCatId(@PathVariable("catelogId") Long catId){
         List<AttrgroupwithattrVo> attrgroupwithattrVos=attrGroupService.getAttrGroupAndAttrByCatId(catId);
         return R.ok().put("data",attrgroupwithattrVos);
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{cateid}")
    //@RequiresPermissions("tlmailproduct:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("cateid") Integer cateid){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,cateid);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("tlmailproduct:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] pathsById = categoryService.getPathsById(attrGroup.getCatelogId());
        attrGroup.setPaths(pathsById);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("tlmailproduct:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("tlmailproduct:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * /product/attrgroup/attr/relation/delete
     * 删除
     */
    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("tlmailproduct:attrgroup:delete")
    public R deleteAttrRelation(@RequestBody AttrattrgrouprelationVo[] attrattrgrouprelationVo){
		//attrGroupService.removeByIds(Arrays.asList(attrGroupIds));
        attrGroupService.removerelation(attrattrgrouprelationVo);
        return R.ok();
    }

}
